package com.whzjkj.gateway.server.tcp.listener;

import com.whzjkj.gateway.constant.JT808Constant;
import com.whzjkj.gateway.server.tcp.handler.TCPServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: Mr.Hu
 * @create: 2018-10-29 16:09
 **/
@Component
@Slf4j
public class TCPServerListener {

    /**
     * 创建bootstrap
     */
    private ServerBootstrap serverBootstrap = null;
    /**
     * BOSS
     */
    private EventLoopGroup bossGroup = null;
    /**
     * Worker
     */
    private EventLoopGroup workerGroup = null;
    @Resource
    private TCPServerHandler tcpServerHandler;

    public void start() {
        serverBootstrap = new ServerBootstrap();
        bossGroup = new NioEventLoopGroup();
        workerGroup = new NioEventLoopGroup();
        serverBootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 128)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        ChannelPipeline pipeline = socketChannel.pipeline();
                        pipeline.addLast(new LoggingHandler(LogLevel.INFO));
                        pipeline.addLast("idleStateHandler",
                                new IdleStateHandler(30, 0, 0, TimeUnit.MINUTES));
                        pipeline.addLast(new DelimiterBasedFrameDecoder(1024, Unpooled.copiedBuffer(new byte[]{JT808Constant.PACKAGE_MARKER}),
                                Unpooled.copiedBuffer(new byte[]{JT808Constant.PACKAGE_MARKER, JT808Constant.PACKAGE_MARKER})));
                        //pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
                        //pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
                        pipeline.addLast(tcpServerHandler);
                    }
                });
        try {
            ChannelFuture channelFuture = serverBootstrap.bind(21001).sync();
            log.info("TCP监听服务启动·······");
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
